package tech.yunland.shop.domain.service;

import com.moyuer.bsf.domain.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tech.yunland.shop.domain.dao.ExamDao;
import tech.yunland.shop.domain.dao.LessonsDao;
import tech.yunland.shop.domain.dao.ScoreDao;
import tech.yunland.shop.domain.model.Score;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

/**
 * Created by v on 17-7-3.
 */
@Service
public class ScoreServiceImpl implements ScoreService {

    @Autowired
    private ScoreDao scoreDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private ExamDao examDao;

    @Override
    public void save(String sid, BigDecimal score) {
        Score score1 = scoreDao.get(sid);
        score1.setScore(score);
        scoreDao.update(score1);
    }

    @Override
    public Integer getSize(String eid) {
        String sql =
                "select count(1) counts\n" +
                        "from score s\n" +
                        "where s.exam_id = '" + eid + "'";
        int counts = ((BigInteger) scoreDao.findUniqueBySQL(sql).get("counts")).intValue();
        return counts;
    }

    @Override
    public List<Score> get(String eid, Integer startIdx, Integer rows) {
        String hql =
                "from Score s\n" +
                        "where s.exam.id = '" + eid + "'";
        return scoreDao.find(hql, startIdx, rows);
    }

    @Override
    public Integer getScoreByDateSize(String desid, String uid) {
        String sql =
                "select count(1) counts\n" +
                        "from score s left join exam e on s.exam_id = e.id\n" +
                        "where e.examdescription_id='" + desid + "'";
        if (uid != null) {
            sql += "and s.students_id='" + uid + "'";
        }
//        sql += "order by e.exam_time desc";
        int counts = ((BigInteger) scoreDao.findUniqueBySQL(sql).get("counts")).intValue();
        return counts;
    }

    @Override
    public List<Score> getScoreByDate(String desid, String uid, Integer startIdx, Integer rows) {
        String hql =
                "from Score s\n" +
                        "where s.exam.examDescription.id='" + desid + "'";
        if (uid != null) {
            hql += "and s.students.id='" + uid + "'";
        }
        hql += "order by s.exam.lessons.lname desc";
        return scoreDao.find(hql, startIdx, rows);
    }
}
